package com.hhu.wangzb.server.util;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于数据库数据导入
 */
public class RequestParamsWrapper {

    public static Object wrap(String value, Class clazz) throws IllegalAccessException, InstantiationException {

        Object o = clazz.newInstance();

        String[] strings = value.split("\n");

        Field[] declaredFields = clazz.getDeclaredFields();

        int j = 0;
        for (int i = 0; i < declaredFields.length; i++) {
            if (j >= strings.length) break;
            Field field = declaredFields[i];
            if (!field.getName().equals("serialVersionUID") && !field.getName().equals("id")) {
                field.setAccessible(true);

                if (field.getType() == Integer.class) {
                    if (strings[j] == null || strings[j].equals("")) field.set(o, null);
                    // 映射关系特殊处理
                    else if (strings[j].equals("可研")) {
                        field.set(o, 1);
                    }
                    else if (strings[j].equals("山地光伏")) {
                        field.set(o, 1);
                    }
                    else if (strings[j].equals("平地光伏")) {
                        field.set(o, 2);
                    }
                    else if (strings[j].equals("屋顶光伏")) {
                        field.set(o, 3);
                    }
                    else if (strings[j].equals("技术评判")) {
                        field.set(o, 4);
                    }
                    else field.set(o, Integer.valueOf(strings[j]));
                    j++;
                }
                else if (field.getType() == Float.class) {
                    if (strings[j] == null || strings[j].equals("")) field.set(o, null);
                    else field.set(o, Float.valueOf(strings[j]));
                    j++;
                }
                else if (field.getType() == String.class) {
                    if (strings[j] == null || strings[j].equals("")) field.set(o, null);
                    else field.set(o, strings[j]);
                    j++;
                }
                else if (field.getType() == Date.class) {
                    if (strings[j] == null || strings[j].equals("")) field.set(o, null);
                    else {
                        String date = strings[j];
                        String year = date.substring(0, date.indexOf("年"));
                        String month = date.substring(date.indexOf("年") + 1, date.indexOf("月"));

                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.YEAR, Integer.parseInt(year));
                        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
                        calendar.set(Calendar.DATE, 1);

                        field.set(o, calendar.getTime());
                    }
                    j++;
                }
            }
        }

        return o;
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Map<String, Object> map = new HashMap<>();

        map.put("value", "虎悦通忻州市忻府区一期5万千瓦光伏发电项目\n" +
                "山地光伏\n" +
                "可研\n" +
                "\n" +
                "\n" +
                "信息产业电子第十一设计研究院科技工程股份有限公司（无锡）\n" +
                "2020年9月\n");
    }
}
